/**
 * 管理员 数据表
 * @des:并初始化数据表的一些基本操作
 * @author:miaohuaiyu
 * Created by mhy on 2018年1月16日13:00:06
 */

$(document).ready(function() {
    //全选按钮
    $("#check-all").attr('onclick','checkAll(this)');
});

/*function:分页
 * @param:$checkbox_paginate
 * @param:pagesNumber 总页数
 * @param:funcName 函数名
 * @param:currentpageNumber 当前页码
 * @param:paginateSize 页码栏宽度
 * */
function createPaging($checkbox_paginate,currentPageNumber,pagesNumber,funcName,paginateSize){
    var $previousPage = $("#datatable-checkbox_previous");
    var $nextPage = $("#datatable-checkbox_next");
    var $liObj;
    var $paginateSize = parseInt(paginateSize);
    currentPageNumber = parseInt(currentPageNumber);

    $previousPage.children("a").attr("onclick",'previousPage(this)');
    $nextPage.children("a").attr("onclick",'nextPage(this)');

    if(pagesNumber == 0){
        $previousPage.addClass("disabled");
        $nextPage.addClass("disabled");
    }else if(pagesNumber <= $paginateSize){
        for(var i = 0;i < pagesNumber;i++){
            $liObj = $("<li class ='paginate_button page'><a href='#'>" + (i+1)+"</a></li>");
            $liObj.children("a").attr("onclick",funcName);
            $nextPage.before($liObj);
        }   //end for
    }else if(pagesNumber > $paginateSize){
        if(currentPageNumber > ($paginateSize/2) && currentPageNumber + ($paginateSize/2) < pagesNumber){
            for(var i = currentPageNumber - 5;i < currentPageNumber + 5;++i){
                $liObj = $("<li class ='paginate_button page'><a href='#'>" + (i+1)+"</a></li>");
                $liObj.children("a").attr("onclick",funcName);
                $nextPage.before($liObj);
            }   //end for
        } else if(currentPageNumber > ($paginateSize/2) && currentPageNumber + ($paginateSize/2) >= pagesNumber){
            for(var i = pagesNumber - $paginateSize;i < pagesNumber;++i){
                $liObj = $("<li class ='paginate_button page'><a href='#'>" + (i+1)+"</a></li>");
                $liObj.children("a").attr("onclick",funcName);
                $nextPage.before($liObj);
            }   //end for
        } else if(currentPageNumber <= ($paginateSize/2)){
            for(var i = 0;i < $paginateSize;++i){
                $liObj = $("<li class ='paginate_button page'><a href='#'>" + (i+1)+"</a></li>");
                $liObj.children("a").attr("onclick",funcName);
                $nextPage.before($liObj);
            }   //end for
        }
    }
    funcName = funcName.substr(0,funcName.indexOf('('));
    $("#pageSize").change(function () {
        var $aTemp = $checkbox_paginate.children("ul").children("li.page").eq(0).children("a").clone();
        $aTemp.text(1);
        $aTemp.trigger("click");
        // var $currentPage = $checkbox_paginate.children("ul").children("li.page").eq(0).children("a");
        // $currentPage.trigger("click");
    })
}

/*function:nextPage*/
function nextPage(e) {
    var $pagination = $(e).parents("ul");
    var $ulObj = $pagination;
    var $currentPage;
    var $liObj = $("li.page",$ulObj);
    var liLength = $pagination.children("li:last-child").prev().text();

    for(var i = 0;i < liLength;i++){
        var pageNumb;
        if($liObj.eq(i).hasClass("active")){
            pageNumb = $liObj.eq(i).children("a").text();
            if(pageNumb == liLength){
                return;
            }
            $currentPage = $liObj.eq(i);
            break;
        }
    }   //end for
    $currentPage.children("a").clone().text(parseInt(pageNumb) + 1).trigger("click");
}

/*function:previousPage*/
function previousPage(e) {
    var $pagination = $(e).parents("ul");
    var $ulObj = $pagination;
    var $currentPage;
    var $liObj = $("li.page",$ulObj);
    var liLength = $pagination.children("li:last-child").prev().text();

    for(var i = 0;i < liLength;i++){
        var pageNumb;
        if($liObj.eq(i).hasClass("active")){
            pageNumb = $liObj.eq(i).children("a").text();
            if(1 == pageNumb){
                return;
            }
            $currentPage = $liObj.eq(i);
            break;
        }
    }   //end for
    $currentPage.children("a").clone().text(parseInt(pageNumb) - 1).trigger("click");
}

/*function:adjustPagination
 * @param:pagesNumber*/
function adjustPagination($pagination,pagesNumber,totalPagesNumber) {
    var $previousPageButton = $("#datatable-checkbox_previous");
    var $nextPageButton = $("#datatable-checkbox_next");
    var $ulObj = $pagination;
    var $liObj = $ulObj.children("li.page");    //页码元素
    var liLength = totalPagesNumber;
    var $firstLiObj = $liObj.eq(0);

    if(liLength == 1 ){
        $liObj.eq(0).addClass("active");
        $previousPageButton.addClass("disabled");
        $nextPageButton.addClass("disabled");
        return;
    } else if(liLength == 0){
        $previousPageButton.addClass("disabled");
        $nextPageButton.addClass("disabled");
        return;
    }

    $liObj.removeClass("active");
    $liObj.eq(pagesNumber - $firstLiObj.text()).addClass("active");

    if(pagesNumber == 1 ){
        $previousPageButton.addClass("disabled");
        $nextPageButton.removeClass("disabled");
    } else if(pagesNumber  == liLength){
        $nextPageButton.addClass("disabled");
        $previousPageButton.removeClass("disabled");
    }else{
        $nextPageButton.removeClass("disabled");
        $previousPageButton.removeClass("disabled");
    }
}

//初始化表格
function initTable(){
    var $datatable = $('#datatable-checkbox');

    $datatable.dataTable( {
        destroy:true,
        "bPaginate": false, //翻页功能
        "bLengthChange": true, //改变每页显示数据数量
        "bFilter": true, //过滤功能
        "bSort": true, //排序功能
        "bInfo": false,//页脚信息
    } );

    var $selectDiv = $("<div class='dataTables_length' id='datatable-checkbox_length'>" +
        "<label>显示 <select name='datatable-checkbox_length' aria-controls='datatable-checkbox' class='form-control input-sm' id='pageSize'>" +
        "<option value='10' selected>10</option><option value='25'>25</option><option value='50'>50</option>" +
        "<option value='100'>100</option><option value='all'>all</option>" +
        "</select> 条目</label>" +
        "</div>");
    $("#datatable-checkbox_wrapper").children("div.row").children("div.col-sm-6").eq(0).append($selectDiv.clone());
}
